
	
this.view = {extend: "base", doctype: "transitional" };

this.handler = function(){
	
	var fetchSubmissions = function(url){
		
		var target = $.ajax({
			url: url || "http://news.ycombinator.com/newest",
			async: false
		}).responseXML;
		
		var moreAnchor = $("table:eq(2) tr:last a", target);
		url = (moreAnchor && moreAnchor.text() === "More")
			? "http://news.ycombinator.com/" + moreAnchor.attr("href")
			: null;
		var items = [];
		$("table:eq(2) tr:not([style])", target).each(function(i, elem){
			
			var item;
			if(i % 2 == 0){
				var titleAnchor = $("td:eq(2) a", elem), titleLink = titleAnchor.attr("href") + "";
				item = {type: "submission"};
				item.title = titleAnchor.text().replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
				if(/^item/.test(titleLink)){
					item.titleLink = "http://news.ycombinator.com/" + titleLink;
					item.self = true;
				} else {
					item.titleLink = titleLink.replace(/&/g, "&amp;");
				}
				items.push(item);
			} else {
				// subtext
				item = items[items.length - 1];
				var td = $("td[class=subtext]:first", elem), anchors = $("a", td),
				userAnchor = $(anchors[0]), discussAnchor = $(anchors[1]),
				points = $("span:first", td).text(),
				timeTokens = $(td).text().match(/(\d+)\s([a-z]+)\sago/),
				timeNum = timeTokens[1], timeType = timeTokens[2],
				time = (timeNum * ((/^h/.test(timeType)) ? 60 : (/^d/.test(timeType)) ? 60 * 24 : 1)) + 1;
				item.id = discussAnchor.attr("href").match(/\?id=(\d+)$/)[1];
				item.userId = userAnchor.attr("href").match(/\?id=(.+)$/)[1];
				item.pubDate = (new Date()).addMilliseconds(-time * 60000);
				item.discuss = discussAnchor.text();
				item.points = points;
							
			}
		});
		items.forEach(function(item){
			if(item.id){
				var savedItem;
				if(!(savedItem = storage.items.getSingle({id: item.id}, true))){
					alert(item.toSource());
					storage.items.insert(item);
				} else {
					savedItem.id = item.id;
					savedItem.title = item.title;
					savedItem.titleLink = item.titleLink;
					savedItem.self = item.self;
					savedItem.discuss = item.discuss;
					savedItem.points = item.points;
					storage.items.update({id: item.id}, savedItem, true);
				}
			}
		});
		
		if(url)
			fetchSubmissions(url);
	}
	
	var fetchComments = function(url){
				
		var target = $.ajax({
			url: url || "http://news.ycombinator.com/newcomments",
			async: false
		}).responseXML;
	
		
		var moreAnchor = $("table:eq(2) tr:last a", target);
		url = (moreAnchor && moreAnchor.text() === "More")
			? "http://news.ycombinator.com/" + moreAnchor.attr("href")
			: null;
		$("table:eq(2) tr:not([style])", target).reverse().each(function(i, elem){
			var commentSpan = $("td:eq(1) span[class=comment]", elem), commentText = commentSpan.text();
			if(commentText){
				var item, td = $("td[class=default]", elem), anchors = $("a", td),
					userAnchor = $(anchors[0]), permalinkAnchor = $(anchors[1]), parentAnchor = $(anchors[2]), topicAnchor = $(anchors[3]),
					id = permalinkAnchor.attr("href").match(/\?id=(\d+)$/)[1], 
					parentId = parentAnchor.attr("href").match(/\?id=(\d+)$/)[1],
					userId = userAnchor.attr("href").match(/\?id=(.+)$/)[1],
					topicId = topicAnchor.attr("href").match(/\?id=(\d+)$/)[1],
					topic = storage.items.getSingle({id: topicId}, true),
					topicText = (topic) ? topic.value.title : topicAnchor.text(), points = $("span:eq(1)", td).text(),
					timeTokens = $("span[class=comhead]", td).text().match(/(\d+)\s([a-z]+)\sago/),
					timeNum = timeTokens[1], timeType = timeTokens[2],
					time = (timeNum * ((/^h/.test(timeType)) ? 60 : (/^d/.test(timeType)) ? 60 * 24 : 1));
				$("div", td).remove();
				$("br:first", td).remove();
				var firstPara = "<p>" + $("font:first", td).remove().html() + "</p>";
				$("span[class=comment]", td).remove();
				var commentHTML = (firstPara + " " + $(td).html()).replace(/\n/g, " ").replace(/&/g, "&amp;"),
				title = commentText.substr(0, 80).replace(/\n/g, " ").replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
				title += (commentText.length > 80) ? "..." : "";
				item = storage.items.getSingle({id: id}, true);
				//alert(parentId)
				if(!item){
					var parentItem = storage.items.getSingle({id: parentId}, true);
					if(parentItem){
						var newItem = {
							title: title,
							type: "comment",
							id: id,
							parentId: parentId,
							toUserId: parentItem.value.userId,
							isUserReply: parentItem.value.self || (parentItem.value.type === "comment"),
							topicId: topicId,
							topicText: topicText,
							userId: userId,
							points: points,
							pubDate: (new Date()).addMilliseconds(-time * 60000),
							commentHTML: commentHTML
						};
						alert(newItem.toSource());
						storage.items.insert(newItem);
					}
				} else {
						
					item.value.title = title;
					item.value.commentHTML = commentHTML;
					item.value.points = points;
					item.value.topicText = topicText;
					storage.items.update({id: id}, item.value, true);
					
				}
			}
		});
		
		if(url)
			fetchComments(url);
	}
	
	this.get = function(){
		
		if(!scanner.started){
			scanner.started = true;
			setTimeout(function(){
					fetchSubmissions();
					fetchComments();
					storage.items.remove({pubDate: {lt: (new Date()).addDays(-21)}});
					scanner.started = false;
					alert("storage size: " + storage.items.size);
					$.get("http://127.0.0.1/hacker-news-feeds/scanner");
				
			}, (request.params.start || 60000) * 7);
						
			$("#content").text("starting...");
		} else {
			$("#content").text("running...");
		}
		//$("#content").append("<br/>" + storage.items.getSingle({id: "272413"}).value.toSource());
		
		
		/*storage.items.get({where:{type: "comment", toUserId: null}}).forEach(function(row){
			var item = row.value;
			
			var parentItem = storage.items.getSingle({id: item.parentId});
			if(parentItem){
			item.toUserId = parentItem.value.userId;
			item.isUserReply = parentItem.value.self || (parentItem.value.type === "comment")
			alert(item.toSource());			
			storage.items.update(item, {id: item.id}, true);
			}
		});*/
		
		
	}
	
	this.post = function(){
		this.get();
	}
}

